﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DALLayer.Base;
using DataDefinition;

namespace DALLayer.Providers
{
    public class TagDataProvider : DataAccessBase
    {
        #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="TagDataProvider" /> class.
        /// </summary>
        public TagDataProvider(){}
        #endregion

        #region [Public Methods]
        /// <summary>
        /// Method gets all the tags or by id
        /// </summary>
        /// <param name="tagId">tag id</param>
        /// <param name="selectById">select by id</param>
        /// <returns>data set</returns>
        public AutoFixDataSet GetAllTags(int tagId, bool selectById)
        {
            AutoFixDataSet dataSet = null;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT Tag.* FROM Tag");
                //if its a selection by id
                if (selectById == true)
                {
                    //Add the sql string to retrieve data based on ArticleID
                    base.CommandTextBuilder.Append(@" WHERE (TagID = @TagID)");
                    //Add the params needed to retrieve data
                    base.DBParams.Add(@"@TagID", tagId);
                }
                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.Tag.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }
     /// <summary>
     /// Method gets all tags by article id
     /// </summary>
     /// <param name="articleID">article id</param>
     /// <param name="isActive">is active</param>
     /// <returns>data set</returns>
        public AutoFixDataSet GetAllTagsByArticleID(int articleID, bool isActive)
        {
            AutoFixDataSet dataSet = null;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"Tag.Tag, Tag.TagID, Tag.IsActive
                                                FROM Tag INNER JOIN (Article INNER JOIN ArticleTag ON Article.ArticleID = ArticleTag.ArticleID) 
                                                ON Tag.TagID = ArticleTag.TagID
                                                WHERE (((Article.ArticleID)=@ArticleID) AND ((Tag.IsActive)=@IsActive))");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@ArticleID", articleID);
                base.DBParams.Add(@"@IsActive", isActive);
                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.Article.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }

        /// <summary>
        /// The method inserts tag
        /// </summary>
        /// <param name="tag">tag</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool InsertTag(string tag, bool isActive)
        {

            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    INSERT INTO Tag(Tag, IsActive)
                    VALUES(@Tag, @IsActive)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@Tag", tag);     
                base.DBParams.Add(@"@IsActive", isActive);

                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception eX)
            {
                success = false;
                //Log here
            }
            //return success
            return success;
        }
        /// <summary>
        /// The method updates tag
        /// </summary>
        /// <param name="tagId">tag id</param>
        /// <param name="tag">tag</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool UpdateTag(int tagId, string tag, bool isActive)
        {
            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    UPDATE Tag
                    SET Tag = @Tag, 
                        IsActive =  @IsActive
                    WHERE TagID = @TagID");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@TagID", tagId);
                base.DBParams.Add(@"@Tag", tag);
                base.DBParams.Add(@"@IsActive", isActive);

                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception eX)
            {
                success = false;
                //Log here
            }
            //return success
            return success;

        }
        /// <summary>
        /// The method inserts tag to the table and returns the identity
        /// </summary>
        /// <param name="tag">tag</param>
        /// <param name="isActive"></param>
        /// <returns>identity/tagid</returns>
        public int InsertTagScalar(string tag, bool isActive)
        {

            int result = 0;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@" INSERT INTO Tag(Tag, IsActive)
                                                VALUES(@Tag, @IsActive)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@Tag", tag);
                base.DBParams.Add(@"@IsActive", isActive);
                //get the result                
                result = base.ExecuteScalar();

            }
            catch (Exception)
            {
                //Log here
            }
            finally
            {
                base.FinalizeConnection();
            }
            //return success
            return result;
        }

        #endregion

    }
}
